package com.example.myapp.fragment;

import com.example.myapp.R;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity implements OnClickListener {
	private long mExitTime;
	private TextView txt_content, txt_message, txt_better, txt_setting;
	private BetterFragment betterFragment;
	private ContentFragment contentFragment;
	private MessageFragment messageFragment;
	private SettingFragment settingFragment;
	private String info;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
        Bundle bundle=getIntent().getExtras();
        info=bundle.getString("info");
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		txt_message = (TextView) findViewById(R.id.txt_message);
		txt_better = (TextView) findViewById(R.id.txt_better);
		txt_content = (TextView) findViewById(R.id.txt_content);
		txt_setting = (TextView) findViewById(R.id.txt_setting);
		txt_better.setOnClickListener(this);
		txt_content.setOnClickListener(this);
		txt_message.setOnClickListener(this);
		txt_setting.setOnClickListener(this);
		Intent intert=getIntent();
		int id = intert.getIntExtra("xxx",1);
		if (id==1) {
			txt_content.performClick();//模拟一次点击，既进去后选择第一项
		}
	}

	private void setDefaultFragment() {
		// TODO Auto-generated method stub
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
		contentFragment = new ContentFragment();
		mFragmentTransaction.replace(R.id.id_content, contentFragment);
		mFragmentTransaction.commit();
	}

	// 重置所有文本的选中状态
	private void setSelected() {
		txt_content.setSelected(false);
		txt_message.setSelected(false);
		txt_better.setSelected(false);
		txt_setting.setSelected(false);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
		switch (v.getId()) {
		case R.id.txt_better:
			setSelected();
			txt_better.setSelected(true);
			if (betterFragment == null) {
				betterFragment = new BetterFragment();
			}
			mFragmentTransaction.replace(R.id.id_content, betterFragment);
			break;
		case R.id.txt_content:
			setSelected();
			txt_content.setSelected(true);
			if (contentFragment == null) {
				contentFragment = new ContentFragment();
			}
			mFragmentTransaction.replace(R.id.id_content, contentFragment);
			break;
		case R.id.txt_message:
			setSelected();
			txt_message.setSelected(true);
			if (messageFragment == null) {
				messageFragment = new MessageFragment();
			}
			mFragmentTransaction.replace(R.id.id_content, messageFragment);
			break;
		case R.id.txt_setting:
			setSelected();
			txt_setting.setSelected(true);
			if (settingFragment == null) {
				settingFragment = new SettingFragment();
			}
            Bundle bundle = new Bundle();    
            bundle.putString("info", info);  
            settingFragment.setArguments(bundle);
			mFragmentTransaction.replace(R.id.id_content, settingFragment);
			break;

		default:
			break;
		}
		mFragmentTransaction.commit();
	}
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (event.getAction() == KeyEvent.ACTION_DOWN
					&& event.getRepeatCount() == 0) {
				if ((System.currentTimeMillis() - mExitTime) > 2000) {// 如果两次按键时间间隔大于2000毫秒，则不退出
					Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
					mExitTime = System.currentTimeMillis();// 更新mExitTime
				} else {
					int id = android.os.Process.myPid();
					if (id != 0) {
						android.os.Process.killProcess(id);
					}
				}
				return true;
			}
		}
		return super.dispatchKeyEvent(event);
	}
}
